package service.impl;

import java.sql.Timestamp;
import java.util.List;

import common.impl.DateHandle;
import common.impl.exception.ServiceException;
import model.MHomework;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.interf.IHomework;
import dao.interf.IHomeworkDao;
import dao.interf.ITeacherDao;
import dao.pojo.Homework;
import dao.pojo.Teacher;

public class HomeworkImpl implements IHomework{
	private IHomeworkDao homeworkDao;
	private ITeacherDao teacherDao;

	public void setHomeworkDao(IHomeworkDao homeworkDao) {
		this.homeworkDao = homeworkDao;
	}

	public void setTeacherDao(ITeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	@Override
	public String add(MHomework model) {
		Homework homework = new Homework();
			
		Teacher tea = teacherDao.getById(model.getTeaId());
		if (tea == null) {
			throw new ServiceException("ERR-0021");
		} else {
			homework.setTeacher(tea);
		}
		homework.setHwName(model.getHwName());
		homework.setHwClasses(model.getHwClasses());
		homework.setHwContent(model.getHwContent());
		homework.setHwDeadline(model.getHwDeadline());
		homework.setHwCreateDate(model.getHwCreateDate());

		return homeworkDao.add(homework);
	}

	@Override
	public String getById(MHomework model) {
		Homework homework = homeworkDao.getById(model.getHwId());
		if(null == homework) {
			throw new ServiceException("ERR-0041");
		}		
		return createHomeworkObj(homework).toString();
	}

	@Override
	public String deleteById(Integer id) {
		homeworkDao.deleteById(id);
		return "1";
	}

	@Override
	public String edit(MHomework model) {
		Homework homework = homeworkDao.getById(model.getHwId());
		if (null == homework) {
			throw new ServiceException("ERR-0041");
		}
		if(homework.getTeacher().getTeaId() != model.getTeaId()) {
			Teacher tea = teacherDao.getById(model.getTeaId());
			if (tea == null) {
				throw new ServiceException("ERR-0021");
			} else {
				homework.setTeacher(tea);
			}
		}
		homework.setHwName(model.getHwName());
		homework.setHwClasses(model.getHwClasses());
		homework.setHwContent(model.getHwContent());
		homework.setHwDeadline(model.getHwDeadline());

		homeworkDao.edit(homework);
		return "1";
	}

	@Override
	public String getAll(Integer teaId) {
		Teacher teacher = teacherDao.getById(teaId);
		if (null == teacher) {
			throw new ServiceException("ERR-0111");
		}
		List<Homework> list = homeworkDao.getListByProperty("teacher.teaId", teacher.getTeaId().toString());

		return createHomeworkArr(list).toString();
	}

	private JSONObject createHomeworkObj(Homework homework) {
		JSONObject obj = new JSONObject();
		obj.put("hwId", homework.getHwId());
		if(null != homework.getTeacher()) {
			obj.put("teaId", homework.getTeacher().getTeaId());
			obj.put("teaName", homework.getTeacher().getTeaName());
		}
		obj.put("hwClasses", homework.getHwClasses());
		obj.put("hwName", homework.getHwName());
//		obj.put("hwCreateDate", DateHandle.timeGetDate(homework.getHwCreateDate()));
//		obj.put("hwDeadline", DateHandle.timeGetDate(homework.getHwDeadline()));
		obj.put("hwCreateDate", homework.getHwCreateDate().toString().substring(0, 19));
		obj.put("hwDeadline", homework.getHwDeadline().toString().subSequence(0, 19));
		obj.put("hwContent", homework.getHwContent());

		Timestamp current = new Timestamp(System.currentTimeMillis());
		int compare = current.compareTo(homework.getHwDeadline());
		if (compare < 0) {
			obj.put("state", 0);
		} else {
			obj.put("state", 1);
		}


		return obj;
	}

	private JSONArray createHomeworkArr(List<Homework> list) {
		JSONArray jarr = new JSONArray();
		if(null != list) {
			for (Homework homework : list) {
				jarr.add(createHomeworkObj(homework));
			}
		}
		return jarr;
	}


}
